Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
shallow-clone
Advanced tools
The shallow-clone npm package is designed to create shallow copies of various JavaScript data types, such as objects, arrays, and other native types. It is useful for cases where you need a new object with the same properties as the original but without deep copying nested structures.
Cloning Objects
Creates a shallow copy of an object. Changes to nested objects in the original will reflect in the clone.
const shallowClone = require('shallow-clone');
const obj = { a: 1, b: { c: 2 } };
const clonedObj = shallowClone(obj);
console.log(clonedObj); // { a: 1, b: { c: 2 } }
Cloning Arrays
Creates a shallow copy of an array. Changes to nested arrays in the original will reflect in the clone.
const shallowClone = require('shallow-clone');
const arr = [1, 2, [3, 4]];
const clonedArr = shallowClone(arr);
console.log(clonedArr); // [1, 2, [3, 4]]
Cloning Dates
Creates a shallow copy of a Date object, resulting in a new Date object with the same time.
const shallowClone = require('shallow-clone');
const date = new Date();
const clonedDate = shallowClone(date);
console.log(clonedDate); // outputs cloned date object
Cloning Regular Expressions
Creates a shallow copy of a RegExp object, including its flags and lastIndex property.
const shallowClone = require('shallow-clone');
const regex = /test/gi;
const clonedRegex = shallowClone(regex);
console.log(clonedRegex); // outputs cloned RegExp object
Lodash's clonedeep method provides deep cloning capabilities, unlike shallow-clone which only performs shallow cloning. This is useful for more complex data structures where nested objects and arrays also need to be independently cloned.
The clone package offers both deep and shallow cloning functionalities. It can be configured to perform either, making it more flexible compared to shallow-clone which only supports shallow cloning.
Creates a shallow clone of any JavaScript value.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install with npm:
$ npm install --save shallow-clone
const clone = require('shallow-clone');
Supports
By default, only the array itself is cloned (shallow), use clone-deep if you also need the elements in the array to be cloned.
const arr = [{ a: 0 }, { b: 1 }];
const foo = clone(arr);
// foo => [{ 'a': 0 }, { 'b': 1 }]
// array is cloned
assert(actual === expected); // false
// array elements are not
assert.deepEqual(actual[0], expected[0]); // true
Only the object is shallow cloned, use clone-deep if you also need the values in the object to be cloned.
console.log(clone({ a: 1, b: 2, c: 3 }));
//=> {a: 1, b: 2, c: 3 }
Clones regular expressions and flags, and preserves the .lastIndex
.
const regex = clone(/foo/g); //=> /foo/g
// you can manually reset lastIndex if necessary
regex.lastIndex = 0;
Simply returns primitives unchanged.
clone(0); //=> 0
clone('foo'); //=> 'foo'
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
You might also be interested in these projects:
Object
constructor. | homepageCommits | Contributor |
---|---|
20 | jonschlinkert |
2 | doowb |
1 | jakub-g |
Jon Schlinkert
Copyright © 2019, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on April 15, 2019.
FAQs
Creates a shallow clone of any JavaScript value.
The npm package shallow-clone receives a total of 3,386,658 weekly downloads. As such, shallow-clone popularity was classified as popular.
We found that shallow-clone demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.